// pages/card/card.js
const ini = require('../../common/init/init.js')
const conf = require('../../common/conf/conf.js')
const Zan = require('../../bower_components/zanui-weapp/dist/index.js');
var thisApp = getApp();
var userInfo = wx.getStorageSync('userInfo')
const wxUserInfo = wx.getStorageSync('wxUserInfo')
Page(Object.assign({}, Zan.Toast,{

  /**
   * 页面的初始数据
   */
  data: {
    musicStatus:"/common/img/play.png",
    user:{
      phone: userInfo.mobile ? userInfo.mobile:"暂未设置",
      wechat: userInfo.wechat?userInfo.wechat:"暂未设置",
      addr: userInfo.city ? userinfo.city:"暂未设置",
      position: userInfo.job ? userInfo.job:'暂未设置',
      userName:userInfo.username?userInfo.username:wxUserInfo.nickname,
      headImgUrl: userInfo.avatar ? userInfo.avatar : wxUserInfo.avatarUrl,
    },
    cardPage:{
      honor:ini.cardPage.honor
    },
    photos: ini.photoPage,
    userInfoData:{
      brower:"",
      collection:"",
      vote:""
    },
    companyInfo:{
      logoUrl:"/common/img/00001.jpg",
      slogen:{
        text1:"诚信专业",
        text2:"服务一流",
        text3:"合作共赢"
      }
    },
    videoInfo:{
      // 'http://wxsnsdy.tc.qq.com/105/20210/snsdyvideodownload?filekey=30280201010421301f0201690402534804102ca905ce620b1241b726bc41dcff44e00204012882540400&bizid=1023&hy=SH&fileparam=302c020101042530230204136ffd93020457e3c4ff02024ef202031e8d7f02030f42400204045a320a0201000400'
      url:'',
      title:""
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log("本地缓存userInfo", userInfo)
    var uid = '';
    if(options.uid){
        uid = options.uid
        console.log("uid1",uid)
    } else if (userInfo.id){
      uid = userInfo.id
      console.log("uid2", uid)
    }
    var _this = this
    var wxUserInfo = wx.getStorageSync('wxUserInfo')
    this.setData({
      'user.userName':wxUserInfo.nickName,
      'user.headImgUrl': wxUserInfo.avatarUrl,
    })
    wx.request({
      url: conf.cardPage.cardInfoUrl+uid,
      method:"GET",
      data:{
        // uid:this.global.userInfo.openid
      },
      dataType:"json",
      success:function(res){
        if(res.statusCode == 200 && res.data.success == 1){
         
          var userInfo = res.data.data
          console.log("!!!!!!!!!!!!!!!!!!!!!!!",userInfo.company.news)
          _this.setData({
            'user.phone':userInfo.mobile,
            'user.position':userInfo.job,
            'user.wechat':userInfo.wechat,
            'user.addr': _this.userAddr(userInfo.addr,options),
            'user.headImg': userInfo.avatar,
            'userInfoData.collection': _this.bigNum(userInfo.collect),
            'userInfoData.brower': _this.bigNum(userInfo.view),
            'userInfoData.vote': _this.bigNum(userInfo.like),
            'companyInfo.logoUrl':userInfo.company.logo,
            'videoInfo.url': userInfo.company.video,
            'videoInfo.title': userInfo.company.video.title,
            products:userInfo.company.product,
            photos: userInfo.photo,
            'cardPage.honor': userInfo.company.news
          })
          //背景音乐播放
          console.log('mp3',userInfo.mp3)
          if(userInfo.mp3.auto == 1 && userInfo.mp3.off == 0){
            wx.playBackgroundAudio({
              dataUrl: userInfo.mp3.resource
            })
            wx.setStorage({
              key: 'musicUrl',
              data: userInfo.mp3.resource,
            })
          }
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    var _app = getApp();
    var _this = this
    return {
      title: _app.globalData.userInfo.username + '的名片',
      path: '/page/user?uid='+ _app.globalData.userInfo.id,
      success: function (res) {
        // 转发成功
        _this.showToast('分享成功');
      },
      fail: function (res) {
        // 转发失败
      }
    }
  },
  //收藏
  collect:function(options){
    wx.request({
      url: conf.cardPage.collectUrl,
      data:{
        userInfo:thisApp.global.userInfo,
        fans_id : thisApp.global.userInfo.id,
        start_id : options.uid
      },
      method:"POST",
      success:function(res){
        this.setData({
          'userInfoData.collection': _this.bigNum(userInfo.collect),
        })
      },
    })
  },
  //点赞
  vote:function(options){
    console.log(options)
    wx.request({
      url: conf.cardPage.voteUrl,
      data: {
        userInfo: thisApp.global.userInfo,
        collectUserId: options.userId,
        vote_id: thisApp.global.userInfo.id,
        start_id:options.uid
      },
      method: "POST",
      success: function (res) {
        this.setData({
          'userInfoData.vote': _this.bigNum(userInfo.like),
        })
      },
    })
  },
  //交换名片
  exchange:function(options){
    var _this = this
    console.log(options)
    wx.request({
      url: conf.cardPage.exchangeUrl,
      data:{

      },
      dataType:"json",
      method:"GET",
      success:function(res){
      }
    })
  },
  save:function(){
   var res =  wx.addPhoneContact({
      firstName: '',//姓氏
      lastName:this.data.user.userName,//名字
      mobilePhoneNumber: this.data.user.phone,
      weChatNumber: this.data.user.wechat,
      title: this.data.user.position//职位
    })
    console.log(res);
  },
  bigNum(num){
    if(num >99){
      return '99+'
    }else{
      console.log("num",num)
      return num
    }
  },
  userAddr(addr,options){
      if(options.uid && addr){
        return addr
      }else if(!options.uid && addr){
        return addr
      }else if(!options.uid && !addr){
        return thisApp.globalData.userInfo.addr
      }else{
        return "该用户暂时没有填写详细地址"
      }
  },
  showToast(content) {
    this.showZanToast(content);
  },
  //停止播放背景音乐
  playMusic:function(){
    var musicStatus =  wx.getBackgroundAudioPlayerState({
      success:res=>{
        return res.status
      }
    })
    if(musicStatus == 1){
      wx.pauseBackgroundAudio()
      this.setData({
        musicStatus:"/common/img/stop.png"
      })
    }else{
      var musicResource = wx.getStorageSync('musicUrl')
      wx.playBackgroundAudio({
        dataUrl: musicResource,
      })
    }
  },
  infoData(data){
      if(!data){
        return "暂未设置"
      }else{
        return data
      }
  },
  makeCall:function(){
    wx.makePhoneCall({
      phoneNumber: userInfo.mobile,
    })
  },
  copyWechat:function(){
    wx.setClipboardData({
      data:userInfo.wechat
    })
  },
  copyAddr: function () {
    wx.setClipboardData({
      data: userInfo.addr
    })
  }
}))